package cn.broadcasereceiver;

import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CalendarView;
import android.widget.TextView;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity implements IOnUpdateListener {
    private Button btnSendWeather;
    private Button btnSendTraffic;
    private MyBroadcastReceiver receiver;

    private Button btnSend;
    private HighReceiver highReceiver;
    private LowReceiver lowReceiver;
    private MidReceiver midReceiver;

    private TextView txtTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

//        OrderBroadcast();
//        DynamicBroadcast();
        TimeBroadcast();
        networkBroadcast();
    }

    //    监听系统网络变化
    private void networkBroadcast() {
        receiver = new MyBroadcastReceiver(MainActivity.this);
        IntentFilter filter = new IntentFilter();
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(receiver, filter);
    }

    //    时间广播
    private void TimeBroadcast() {
//        receiver = new MyBroadcastReceiver(MainActivity.this);
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_TIME_CHANGED);
        filter.addAction(Intent.ACTION_TIME_TICK);
        filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
        registerReceiver(receiver, filter);

        txtTime = findViewById(R.id.txt_time);
        updateTime();
    }

    @Override
    public void updateTime() {
        Calendar calendar = Calendar.getInstance();
        int hour = calendar.get(Calendar.HOUR);
        int min = calendar.get(Calendar.MINUTE);
        txtTime.setText(hour + ":" + min);
    }


    //    有序广播
    private void OrderBroadcast() {
        IntentFilter hFilter = new IntentFilter();
        hFilter.addAction("Weather_Report");
        hFilter.setPriority(100);
        highReceiver = new HighReceiver();
        registerReceiver(highReceiver, hFilter);

        IntentFilter lFilter = new IntentFilter();
        lFilter.addAction("Weather_Report");
        lFilter.setPriority(100);
        lowReceiver = new LowReceiver();
        registerReceiver(lowReceiver, lFilter);

        IntentFilter mFilter = new IntentFilter();
        mFilter.addAction("Weather_Report");
        mFilter.setPriority(100);
        midReceiver = new MidReceiver();
        registerReceiver(midReceiver, mFilter);

        btnSend = findViewById(R.id.btn_send);
        btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction("Weather_Report");
                sendOrderedBroadcast(intent, null);
            }
        });
    }


    //    动态注册广播
    private void DynamicBroadcast() {
        receiver = new MyBroadcastReceiver(MainActivity.this);
        IntentFilter filter = new IntentFilter();
        filter.addAction("Weather_Report");
        filter.addAction("Traffic_Report");
        registerReceiver(receiver, filter);

        btnSendWeather = findViewById(R.id.btn_send_weather);
        btnSendWeather.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction("Weather_Report");
                intent.putExtra("Weather", "sunny");
                intent.putExtra("temperature", 20);
                sendBroadcast(intent);
            }
        });

        btnSendTraffic = findViewById(R.id.btn_send_traffic);
        btnSendTraffic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction("Traffic_Report");
                intent.putExtra("Traffic", "堵车");
                intent.putExtra("Time", 30);
                sendBroadcast(intent);
            }
        });
    }

    @Override
    protected void onDestroy() {
        //动态注册要注销广播
        unregisterReceiver(receiver);
        super.onDestroy();
    }
}
